﻿package mortal.game.view.chat.chatPrivate
{
    import Message.Game.*;
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.global.*;
    import com.gengine.keyBoard.*;
    import com.gengine.utils.pools.*;
    import com.mui.containers.*;
    import com.mui.containers.globalVariable.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import extend.language.*;
    import fl.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.font.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.scene3D.player.entity.*;
    import mortal.game.utils.*;
    import mortal.game.view.chat.*;
    import mortal.game.view.chat.chatPanel.*;
    import mortal.game.view.chat.chatViewData.*;
    import mortal.game.view.chat.data.*;
    import mortal.game.view.chat.selectPanel.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.button.*;
    import mortal.game.view.guild.redPack.*;
    import mortal.mvc.core.*;

    public class ChatBaseWindow extends BaseWindow
    {
        protected var _textTitle:TextField;
        protected var _btnMinimize1:GLoadedButton;
        protected var _contentBitmap:ScaleBitmap;
        protected var _tipTextField:GTextFiled;
        protected var _tipBmp:GImageBitmap;
        protected var _scrollPane:GScrollPane;
        protected var _chatBox:ChatPrivatePanel;
        protected var _inputText:GTextInput;
        protected var _spMiddle:Sprite;
        protected var _areaFuns:GBox;
        protected var _btnFace:GLoadingButton;
        protected var _btnColorSelector:GLoadingButton;
        protected var _btnMapPoint:GLoadingButton;
        protected var _btnBrush:GLoadingButton;
        protected var _colorSelector:ColorSelector;
        protected var _tfTips:TextField;
        protected var _btnTips:TextField;
        protected var _btnClose:GButton;
        protected var _btnSend:TimeButton;
        protected var _rightSprite:Sprite;
        protected var _chatIcon:ChatIconBase;
        protected var _sendMapPoint:Array;
        protected var _isHasMapPoint:Boolean = false;
        private var _topTipData:Object;
        protected var _color:uint = 16645629;

        public function ChatBaseWindow()
        {
            this._sendMapPoint = new Array();
            this.init();
            return;
        }// end function

        override protected function configParams() : void
        {
            _contentX = blurLeft;
            _contentY = blurTop;
            paddingBottom = 6;
            paddingLeft = 6;
            paddingRight = 5;
            return;
        }// end function

        override protected function setWindowCenter() : void
        {
            return;
        }// end function

        protected function init() : void
        {
            this.setSize(555, 464);
            this.titleHeight = 30;
            this.initTargetIcon();
            this.create();
            this.addEventListeners();
            return;
        }// end function

        protected function initTargetIcon() : void
        {
            this._chatIcon = new ChatIconBase();
            this._chatIcon.targetWindow = this;
            return;
        }// end function

        protected function create() : void
        {
            this._textTitle = UIFactory.titleTextField("");
            this.title = this._textTitle;
            this._textTitle.y = 5;
            this._btnMinimize1 = UIFactory.gLoadedButton(ImagesConst.MinimizeButton_upSkin, 504 - _contentTopOf3DSprite.x + _contentX, 9 - _contentTopOf3DSprite.y + _contentY, 22, 20, contentTopOf3DSprite);
            this._contentBitmap = ResourceConst.getScaleBitmap("PanelBg");
            this._contentBitmap.x = 7;
            this._contentBitmap.y = 34;
            this._contentBitmap.setSize(363, 430);
            this.addChild(this._contentBitmap);
            UIFactory.bg(7, 34, 363, 20, this, ImagesConst.ChatPrivateTipBg);
            this._tipTextField = UIFactory.gTextField(Language.getString(11501), 47, 34, 320, 20, this);
            this._tipTextField.visible = false;
            this._tipBmp = UIFactory.gImageBitmap("ChatPrivateTips.png", 9, 32, this);
            this._chatBox = new ChatPrivatePanel(40);
            this._chatBox.x = 20;
            this._chatBox.y = 60;
            this.addChild(this._chatBox);
            this._scrollPane = new GScrollPane();
            this._scrollPane.x = 20;
            this._scrollPane.y = 55;
            this._scrollPane.width = 348;
            this._scrollPane.height = 270;
            this._scrollPane.styleName = "GScrollPane";
            this._scrollPane.verticalScrollPolicy = ScrollPolicy.AUTO;
            this._scrollPane.horizontalScrollPolicy = ScrollPolicy.OFF;
            this._scrollPane.source = this._chatBox;
            this.addChild(this._scrollPane);
            this._scrollPane.update();
            this._spMiddle = new Sprite();
            UIFactory.setObjAttri(this._spMiddle, 12, 326, -1, -1, this);
            UIFactory.bg(-5, 0, 363, 24, this._spMiddle, ImagesConst.ChatPrivateToolBg);
            this._areaFuns = new GBox();
            this._areaFuns.x = 3;
            this._areaFuns.horizontalGap = -2;
            this._areaFuns.direction = GBoxDirection.HORIZONTAL;
            this._spMiddle.addChild(this._areaFuns);
            this._inputText = new GTextInput();
            this._inputText.x = 8;
            this._inputText.y = 350;
            this._inputText.setSize(363, 69);
            this._inputText.styleName = "NoSkinInput";
            this._inputText.maxChars = 50;
            this._inputText.mouseFocusEnabled = false;
            this._inputText.textField.wordWrap = true;
            this.addChild(this._inputText);
            this._inputText.drawNow();
            var _loc_1:* = new GTextFormat(FontUtil.defaultName, 12, 11661311);
            _loc_1.leading = 3;
            _loc_1.align = TextFormatAlign.LEFT;
            this._inputText.setStyle("textPadding", 5);
            this._inputText.setStyle("textFormat", _loc_1);
            this.createFuns();
            this.createBottom();
            this.createRightPart();
            return;
        }// end function

        public function get topTipData() : Object
        {
            return this._topTipData;
        }// end function

        public function setTopTipHtmlText(param1:String, param2:Object = null) : void
        {
            this._topTipData = param2;
            this._tipTextField.htmlText = param1;
            this._tipTextField.addEventListener(TextEvent.LINK, this.onTextLink);
            this._tipTextField.visible = true;
            this._tipBmp.visible = false;
            return;
        }// end function

        private function onTextLink(event:TextEvent) : void
        {
            var _loc_2:SRedEnvelopeInfo = null;
            var _loc_3:RedPackData = null;
            if (this._topTipData is SRedEnvelopeInfo)
            {
                _loc_2 = this._topTipData as SRedEnvelopeInfo;
                _loc_3 = new RedPackData();
                _loc_3.bossName = _loc_2.boss.name;
                _loc_3.packId = _loc_2.redId;
                Dispatcher.dispatchEvent(new DataEvent(EventName.RedPackGet, _loc_3));
            }
            return;
        }// end function

        public function cancelTopTipHtmlText() : void
        {
            this._tipTextField.htmlText = Language.getString(11501);
            this._tipTextField.removeEventListener(TextEvent.LINK, this.onTextLink);
            this._tipTextField.visible = false;
            this._tipBmp.visible = true;
            return;
        }// end function

        protected function createFuns() : void
        {
            this._btnFace = UIFactory.gLoadingButton(ResFileConst.ChatTrumpetFaceBtn, 0, 0, 24, 24, this._areaFuns);
            this._btnColorSelector = UIFactory.gLoadingButton(ResFileConst.ChatTrumpetColorBtn, 0, 0, 24, 24, this._areaFuns);
            this._btnMapPoint = UIFactory.gLoadingButton(ResFileConst.ChatPointBtn, 0, 0, 24, 24, this._areaFuns);
            this._btnBrush = UIFactory.gLoadingButton(ResFileConst.ChatClearBtn, 0, 0, 24, 24, this._areaFuns);
            this._btnFace.toolTipData = Language.getString(11502);
            this._btnColorSelector.toolTipData = Language.getString(11503);
            this._btnMapPoint.toolTipData = Language.getString(11504);
            this._btnBrush.toolTipData = Language.getString(11505);
            return;
        }// end function

        protected function createBottom() : void
        {
            this._btnClose = new GButton();
            this._btnClose.width = 50;
            this._btnClose.height = 22;
            this._btnClose.styleName = "ButtonNew";
            this._btnClose.x = 259;
            this._btnClose.label = Language.getString(11506);
            this._btnClose.y = 438;
            this._btnClose.textField.filters = [FilterConst.nameGlowFilter];
            this.addChild(this._btnClose);
            this._btnSend = UIFactory.timeButton(Language.getString(11507), 311, 438, 50, 22, this, Cache.instance.cd.CHAT);
            this._btnSend.isByClick = false;
            this._btnSend.cdTime = ChatUtil.chatCd;
            return;
        }// end function

        protected function createRightPart() : void
        {
            this._rightSprite = new Sprite();
            this._rightSprite.x = 372;
            this._rightSprite.y = 34;
            this.addChild(this._rightSprite);
            var _loc_1:* = ResourceConst.getScaleBitmap("PanelBg");
            _loc_1.x = 0;
            _loc_1.y = 0;
            _loc_1.setSize(180, 430);
            this._rightSprite.addChild(_loc_1);
            UIFactory.bitmap(ImagesConst.SkillTitle_nameBg, 2, 2, this._rightSprite);
            return;
        }// end function

        protected function addEventListeners() : void
        {
            this._btnMinimize1.addEventListener(MouseEvent.CLICK, this.minimizehandler);
            this._inputText.addEventListener(KeyboardEvent.KEY_DOWN, this.keyHandler);
            this._btnSend.addEventListener(MouseEvent.CLICK, this.sendHandler);
            this._btnClose.addEventListener(MouseEvent.CLICK, this.closeHandler);
            this._btnBrush.addEventListener(MouseEvent.CLICK, this.onBtnBrushClick);
            this._btnMapPoint.addEventListener(MouseEvent.CLICK, this.onMapPointClick);
            FacePanel.registBtn(this._btnFace, this.selectFace);
            ColorSelector.registBtn(this._btnColorSelector, this.selectColor);
            this.addEventListener(Event.ADDED_TO_STAGE, this.onAddToStage);
            return;
        }// end function

        protected function keyHandler(event:KeyboardEvent) : void
        {
            if (event.keyCode == KeyCode.ENTER)
            {
                this.SendMsg();
            }
            return;
        }// end function

        protected function sendHandler(event:MouseEvent) : void
        {
            this.SendMsg();
            this._sendMapPoint = new Array();
            this._isHasMapPoint = false;
            return;
        }// end function

        protected function getSendContent() : String
        {
            var _loc_2:int = 0;
            var _loc_3:ChatShowPoint = null;
            var _loc_4:String = null;
            var _loc_1:* = this._inputText.text;
            if (this._sendMapPoint.length > 0)
            {
                for each (_loc_3 in this._sendMapPoint)
                {
                    
                    _loc_4 = _loc_3.mapNameString();
                    if (_loc_4)
                    {
                        _loc_2 = _loc_1.indexOf(_loc_4);
                        if (_loc_2 >= 0)
                        {
                            _loc_1 = _loc_1.replace(_loc_4, _loc_3.mapIdString());
                            this._isHasMapPoint = true;
                        }
                    }
                }
            }
            return _loc_1;
        }// end function

        protected function SendMsg() : void
        {
            return;
        }// end function

        protected function selectColor(param1:uint) : void
        {
            this._color = param1;
            this.setInputTextFormat();
            return;
        }// end function

        protected function setInputTextFormat() : void
        {
            var _loc_1:* = new GTextFormat(FontUtil.songtiName, 12, this._color);
            _loc_1.leading = 3;
            this._inputText.setStyle("textFormat", _loc_1);
            this._inputText.setStyle("textPadding", 5);
            return;
        }// end function

        public function clearMsg() : void
        {
            this._chatBox.reset();
            return;
        }// end function

        protected function onBtnBrushClick(event:MouseEvent) : void
        {
            this._chatBox.reset();
            if (!this.isHide)
            {
                this.timeOutUpdate();
            }
            return;
        }// end function

        protected function onMapPointClick(event:MouseEvent) : void
        {
            var _loc_2:* = new ChatShowPoint(MapFileUtil.mapID, RolePlayer.instance.x2d, RolePlayer.instance.y2d);
            this._inputText.appendText(_loc_2.mapNameString());
            this._inputText.setFocus();
            this._inputText.setSelection(this._inputText.text.length, this._inputText.text.length);
            this._sendMapPoint.push(_loc_2);
            return;
        }// end function

        protected function selectFace(param1:Object) : void
        {
            this._inputText.appendText("/" + param1.toString() + " ");
            this._inputText.setFocus();
            this._inputText.setSelection(this._inputText.text.length, this._inputText.text.length);
            return;
        }// end function

        protected function closeHandler(event:MouseEvent) : void
        {
            this.removeThis();
            return;
        }// end function

        protected function removeThis() : void
        {
            this.hide();
            ChatManager.removeChatWindow(this);
            this._chatIcon.hide();
            return;
        }// end function

        public function addChatItem(param1:ChatData) : void
        {
            var chatItemData:ChatItemData;
            var chatItem:ChatItem;
            var addChatItem2:Function;
            var chatData:* = param1;
            addChatItem2 = function () : void
            {
                _chatBox.addChatItem(chatItem, chatData);
                chatItemData.dispose();
                if (!this.isHide)
                {
                    timeOutUpdate();
                }
                return;
            }// end function
            ;
            chatItemData = ObjectPool.getObject(ChatItemData);
            chatItemData.type = ChatMessageWorking.getChatTypeByAreaName(ChatArea.Secret);
            var cellVector:* = new Vector.<ChatCellData>;
            if (chatData.chatColor == 0)
            {
                chatData.chatColor = 16645629;
            }
            chatData.titleColor = chatData.chatColor;
            if (chatData.alyzType == 1)
            {
                if (chatData.sChatMsg.chatType == EChatType._EChatTypeGuildBattle)
                {
                    ChatMessageWorking.addGuildBattleCellData(cellVector);
                }
                cellVector = cellVector.concat(ChatMessageWorking.getCellDatasByAnalyzeRumor(chatData.content, []));
            }
            else if (chatData.sChatMsg.chatType == EChatType._EChatTypeGuildRoll)
            {
                cellVector = cellVector.concat(ChatMessageWorking.getDiceCellDatas(chatData.chatFromPlayerName, chatData.content));
            }
            else
            {
                cellVector = cellVector.concat(ChatMessageWorking.getCellDatas(chatData));
            }
            chatItemData.cellVector = cellVector;
            chatItem = ObjectPool.getObject(ChatItem);
            chatItem.init(chatItemData.getContentElement(), 240, 22);
            setTimeout(addChatItem2, 40);
            if (chatData.sChatMsg.chatType != EChatType._EChatTypeGuildRoll)
            {
                this._chatIcon.getMsg();
            }
            return;
        }// end function

        public function minimizehandler(event:MouseEvent) : void
        {
            this.hide();
            return;
        }// end function

        public function get inputText() : GTextInput
        {
            return this._inputText;
        }// end function

        public function getChatPlayerEntityId() : SEntityId
        {
            return new SEntityId();
        }// end function

        override public function set visible(param1:Boolean) : void
        {
            super.visible = param1;
            if (param1)
            {
                this.timeOutUpdate();
            }
            return;
        }// end function

        protected function onAddToStage(event:Event) : void
        {
            this.reSetScrollPanePosition();
            return;
        }// end function

        protected function timeOutUpdate() : void
        {
            setTimeout(this.reSetScrollPanePosition, 100);
            return;
        }// end function

        public function reSetScrollPanePosition() : void
        {
            if (this._scrollPane && Global.stage.contains(this._scrollPane))
            {
                this._chatBox.validateNow();
                setTimeout(this.validatePos, 50);
            }
            return;
        }// end function

        protected function validatePos() : void
        {
            this._scrollPane.validateNow();
            this._scrollPane.update();
            this._scrollPane.verticalScrollPosition = this._scrollPane.maxVerticalScrollPosition;
            return;
        }// end function

        override protected function addCloseButton() : void
        {
            _closeBtn = UIFactory.gLoadedButton(ImagesConst.ChatCloseBtn_upSkin, 0, 0, 18, 16, null);
            _closeBtn.focusEnabled = true;
            _closeBtn.name = "Window_Btn_Close";
            _closeBtn.configEventListener(MouseEvent.CLICK, this.closeBtnClickHandler);
            $addChild(_closeBtn);
            return;
        }// end function

        override protected function closeBtnClickHandler(event:MouseEvent) : void
        {
            super.closeBtnClickHandler(event);
            ChatManager.removeChatWindow(this);
            this._chatIcon.hide();
            return;
        }// end function

        override protected function updateBtnSize() : void
        {
            if (_closeBtn)
            {
                _closeBtn.x = this.width - _closeBtn.width - 11;
                _closeBtn.y = 13;
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            paddingBottom = 6;
            paddingLeft = 6;
            paddingRight = 5;
            return;
        }// end function

    }
}
